package com.attilax.util;

import java.io.File;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.attilax.core.ForeachFunctionV2;
import com.attilax.io.DirService;
import com.attilax.io.DirTraveService;
import com.attilax.io.FileService;
import com.attilax.io.FileUtilsAti;
import com.attilax.web.es.esUtil;
import com.google.common.collect.Maps;


//  codescanner
public class codelibutil {
	public static final Logger logger = LoggerFactory.getLogger(codelibutil.class);

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	public static void codescan(String url, String codedir, String codeProcessedMoveDir)
			throws InterruptedException {
		while (true) {
			DirTraveService.traveV6_vS531(new File(codedir), new ForeachFunctionV2() {

				@Override
				public void each(int count, File strFileName, int deep) throws Exception {
					if (!strFileName.getAbsolutePath().trim().endsWith(".java")) {
						FileService.move(strFileName.getAbsolutePath(), codeProcessedMoveDir, codedir);
						return;
					}
					String t = FileUtilsAti.readFileToStringAutoDetectEncode(strFileName);
					logger.info(strFileName.getAbsolutePath());
					Map  m=Maps.newLinkedHashMap();m.put("txt", t);
					String r = esUtil.addData_post(url, JSON.toJSONString(m));
				//	String r = esUtil.addData_post(url, t);
					logger.info(r);
					FileService.move(strFileName.getAbsolutePath(), codeProcessedMoveDir, codedir);

				}
			});

			Thread.sleep(5000);
			logger.info("--hreat   break");
		}
	}
	// public static Logger logger = Logger.getLogger(Cls1.class);
	public static void moveDirSubEmptyDir(String dirRoot, String destDirRoot) {
		new DirTraveService().trave_allSubFile_IncDir(new File(dirRoot), new ForeachFunctionV2() {

			@Override
			public void each(int count, File strFileName, int deep) throws Exception {
				if (strFileName.getName().equals("aaa"))
					System.out.println("d");
				logger.info(strFileName.getAbsolutePath());
				if (strFileName.isDirectory())
					if (strFileName.listFiles().length == 0) {
						DirService.moveDir2dest(strFileName, destDirRoot,dirRoot);
						logger.info("moveDir2dest :" + strFileName.getAbsolutePath());
					}

				if (strFileName.isFile()) {

					if (strFileName.getName().startsWith(".")
							|| FilenameUtils.getExtension(strFileName.getAbsolutePath()).length() == 0) {
						// FileUtilsAti.nameStartWithDot(strFileName)
						// strFileName.getAbsolutePath().startsWith("\\.")
						logger.info("move file to dest :" + strFileName.getAbsolutePath());
						FileUtilsAti.move(strFileName.getAbsolutePath(), destDirRoot, dirRoot);
					}
					if ((!FilenameUtils.getExtension(strFileName.getAbsolutePath()).equals("java"))) {
						logger.info("move file to dest :" + strFileName.getAbsolutePath());
						FileUtilsAti.move(strFileName.getAbsolutePath(), destDirRoot, dirRoot);
					}
					// FileUtilsAti.moveFileToDirectory(strFileName, new
					// File(destDirRoot), true);

				}

			}
		});

	}

}
